#include <QStringList>
#include <QDir>

#include <UIKit/UIApplication.h>
#include <UIKit/UIDialogView.h>
#include <UIKit/UIWindow.h>
#include <UIKit/UIDevice.h>
#include <UIKit/UIEvent.h>

#include "MainViewController.h"
#include "PageItemsView.h"
#include "AppIconView.h"
#include "DockItem.h"

#define DOCK_ICON_OFFSET    (20)

static void withCenterItems (const QList<QGraphicsItem *>& itemList, qreal w) {
    qreal size = 0;
    foreach (QGraphicsItem *item, itemList)
        size += item->boundingRect().width() + 10.0f;

    qreal x = (w - size) / 2;
    foreach (QGraphicsItem *item, itemList) {
        item->setPos(x, item->y());
        x += item->boundingRect().width() + 10.0f;
    }
}

static QImage loadAppIcon (const QDir& appDir, const QString& appBoundle) {
    QString appIconPath = QString("%1/AppIcon.png").arg(appBoundle);
    QImage appIcon = QImage(appDir.filePath(appIconPath));
    if (appIcon.isNull())
        return(QImage(":Resources/DefaultAppIcon.png"));
    return(appIcon);
}

MainViewController::MainViewController (QObject *parent)
    : UIViewController(parent)
{
    UIDevice *device = UIDevice::currentDevice();
    device->beginGeneratingDeviceOrientationNotifications();
}

MainViewController::~MainViewController() {
}

void MainViewController::viewDidLoad (void) {
    UIApplication *application = UIApplication::sharedApplication();

    UIWindow *window = new UIWindow;
    application->setKeyWindow(window);

    connect(&m_signalMapper, SIGNAL(mapped(const QString&)),
            application, SLOT(loadByName(const QString&)));

    m_dockItem = new DockItem;
    m_dockItem->setSize(640, 91);
    m_dockItem->setZValue(0.5f);
    window->addItem(m_dockItem);

    m_pagesView = new PageItemsView;    
    window->addItem(m_pagesView);

    AppIconView *iconView;

    // Scan for Page Apps.
    QDir appDir(UIApplication::defaultAppDirectory());
    appDir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
    QStringList appsList = appDir.entryList(QStringList() << "*.app");
    foreach (QString appBoundle, appsList) {
        if (appBoundle == "Homescreen.app")
            continue;

        QString appName = appBoundle.mid(0, appBoundle.size() - 4);
        QImage appIcon = loadAppIcon(appDir, appBoundle);

        iconView = new AppIconView(appIcon, appName);
        connect(iconView, SIGNAL(doubleTap(UITouch *)), 
                &m_signalMapper, SLOT(map()));
        m_signalMapper.setMapping(iconView, appName);
        m_pagesView->addIconView(iconView);
    }

    // Scan for Dock Apps.
    if (appDir.cd("Dock")) {
        appDir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
        appsList = appDir.entryList(QStringList() << "*.app");
        foreach (QString appBoundle, appsList) {
            if (appBoundle == "Homescreen.app")
                continue;

            QString appName = appBoundle.mid(0, appBoundle.size() - 4);
            QImage appIcon = loadAppIcon(appDir, appBoundle);

            iconView = new AppIconView(appIcon, appName, m_dockItem);
            connect(iconView, SIGNAL(doubleTap(UITouch *)), 
                    &m_signalMapper, SLOT(map()));
            m_signalMapper.setMapping(iconView, appName);
            iconView->setPos(0.0f, -DOCK_ICON_OFFSET);
            iconView->setSize(100.0f, 100.0f);
            iconView->setZValue(1.0f);
        }
    }

    // Show Current Page
    orientationChanged();
}

void MainViewController::viewDidUnload (void) {
    delete m_pagesView;
    delete m_dockItem;
}

void MainViewController::orientationChanged (void) {
    qreal pageHeight = kUIDeviceScreenHeight - m_dockItem->height();

    m_pagesView->setSize(kUIDeviceScreenWidth, pageHeight - DOCK_ICON_OFFSET);
    m_pagesView->changeOrientation(UIInterfaceOrientationIsLandscape(interfaceOrientation()));

    m_dockItem->setPos(0, pageHeight);
    withCenterItems(m_dockItem->childItems(), kUIDeviceScreenWidth);
}

bool MainViewController::shouldAutorotate (UIInterfaceOrientation orientation) const {
    Q_UNUSED(orientation)
    return(true);
}

